package cn.luoshaocheng.pet.service.impl;

import cn.luoshaocheng.basic.PocketMQUtil.RocketMQProducer;
import cn.luoshaocheng.basic.exception.BusinessException;
import cn.luoshaocheng.basic.service.IBaiduAiAuditService;
import cn.luoshaocheng.basic.service.impl.BaiduAiAuditServiceImpl;
import cn.luoshaocheng.basic.util.CodeGenerateUtils;
import cn.luoshaocheng.basic.util.DistanceUtils;
import cn.luoshaocheng.basic.util.PageList;
import cn.luoshaocheng.basic.util.Point;
import cn.luoshaocheng.order.domain.OrderPetAcquisition;
import cn.luoshaocheng.order.mapper.OrderPetAcquisitionMapper;
import cn.luoshaocheng.org.domain.Employee;
import cn.luoshaocheng.org.domain.Shop;
import cn.luoshaocheng.org.mapper.EmployeeMapper;
import cn.luoshaocheng.org.mapper.ShopMapper;
import cn.luoshaocheng.pet.domain.Pet;
import cn.luoshaocheng.pet.domain.SearchMasterMsg;
import cn.luoshaocheng.pet.domain.SearchMasterMsgAuditLog;
import cn.luoshaocheng.pet.dto.AcceptDto;
import cn.luoshaocheng.pet.mapper.PetMapper;
import cn.luoshaocheng.pet.mapper.SearchMasterMsgAuditLogMapper;
import cn.luoshaocheng.pet.mapper.SearchMasterMsgMapper;
import cn.luoshaocheng.pet.query.SearchMasterMsgQuery;
import cn.luoshaocheng.pet.service.ISearchMasterMsgService;
import cn.luoshaocheng.basic.service.impl.BaseServiceImpl;
import cn.luoshaocheng.user.domain.User;
import cn.luoshaocheng.user.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import springfox.documentation.annotations.ApiIgnore;

import java.nio.charset.StandardCharsets;
import java.util.Date;

/**
 * 业务实现类：
 */
@Service
public class SearchMasterMsgServiceImpl extends BaseServiceImpl<SearchMasterMsg> implements ISearchMasterMsgService {

    @Autowired
    private UserMapper userMapper;
    @Autowired
    private ShopMapper shopMapper;

    @Autowired
    private PetMapper petMapper;

    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private SearchMasterMsgMapper searchMasterMsgMapper;

    @Autowired
    private IBaiduAiAuditService baiduAiAuditService;

    @Autowired
    private SearchMasterMsgAuditLogMapper searchMasterMsgAuditLogMapper;

    @Autowired
    private OrderPetAcquisitionMapper orderPetAcquisitionMapper;

    @Autowired
    private RocketMQProducer rocketMQProducer;
    @Override
    public void publish(SearchMasterMsg smm, Long id) {
        //1.获取登录人id
        //2.获取最近店铺id - 百度地图
        //3.将寻主消息添加到数据库
        //4.发送一条短信给门店管理员 - 通知去收宠物
        User user=userMapper.findByLogininfo(id);
        smm.setUserId(user.getId());

        //  shop_id: 先计算最近店铺
        // 获取当前地址的point
        Point point = DistanceUtils.getPoint(smm.getAddress());
        //查询出所有得电铺
        Shop nearestShop = DistanceUtils.getNearestShop(point, shopMapper.loadAll());
        //将寻主得信息发送给最近得店铺
        smm.setShopId(nearestShop.getId());

        searchMasterMsgMapper.save(smm);


        //审核
        Boolean textBoolean = baiduAiAuditService.textAudit(smm.getTitle()+smm.getName() + smm.getAddress() + smm.getCoatColor());
     //   Boolean imgBoolean = baiduAiAuditService.imageAudit(smm.getResources());
        if(textBoolean ){
            smm.setState(1);
            smm.setNote("待处理！");
            searchMasterMsgMapper.update(smm);

        // 2 保存审核日志
        SearchMasterMsgAuditLog smmml = new SearchMasterMsgAuditLog();
        smmml.setState(0);
        smmml.setMsgId(smm.getId());
        smmml.setAuditTime(new Date());
        smmml.setNote("待处理状态！");
        searchMasterMsgAuditLogMapper.save(smmml);
        }else {
            //1修改状态
            //2添加审核日志
            smm.setNote("驳回！数据异常！");
            searchMasterMsgMapper.update(smm);

            // 2 保存审核日志
            SearchMasterMsgAuditLog smmml = new SearchMasterMsgAuditLog();
            smmml.setState(1);
            smmml.setMsgId(smm.getId());
            smmml.setAuditTime(new Date());
            smmml.setNote("审核不通过，驳回，数据异常！");
            searchMasterMsgAuditLogMapper.save(smmml);
            throw new BusinessException("信息异常，请重新输入！");
        }
        Long id1 = smm.getId();
        String s = Long.toString(id1);
        System.out.println(new Date().toLocaleString());
        rocketMQProducer.sendDelayMsg(s,3);
        System.out.println(88);
    }

    @Override
    public PageList<SearchMasterMsg> toHandle(SearchMasterMsgQuery query, Long id) {
        Employee emp = employeeMapper.loadByLoginInfoId(id);
        query.setState(1);
        if(emp.getShop_id()!=null){
            query.setShopId(emp.getShop_id());
        }

        return super.queryPage(query);
    }

    @Override
    public void reject(Long id) {
        searchMasterMsgMapper.reject(id);
    }

    @Override
    public void accept(AcceptDto dto, Long id) {
        Long msgId = dto.getMsgId();
        String note = dto.getNote();
        Long handler = dto.getHandler();
        // 根据smmid查询订单，如果查的到，该订单正在处理中，
        OrderPetAcquisition order= orderPetAcquisitionMapper.loadBySmmId(msgId);
        if(order!=null){
            throw  new BusinessException("亲！订单已经被他人截取~~~~~~");
        }
        //  1. 生成一个pet : 根据searchMasterMsg生成。
        SearchMasterMsg searchMasterMsg = searchMasterMsgMapper.loadById(msgId);

        Pet pet=SearchMasterMsg2Pet(searchMasterMsg);
        petMapper.save(pet);

        //随机生成一个收购订单
        OrderPetAcquisition opa = ininOrderPetAcquisition(dto,pet,searchMasterMsg);
        // 3. 保存订单
        orderPetAcquisitionMapper.save(opa);



    }

    private OrderPetAcquisition ininOrderPetAcquisition(AcceptDto dto, Pet pet, SearchMasterMsg searchMasterMsg) {
        //创建一个order对象
        OrderPetAcquisition order = new OrderPetAcquisition();
        // 随机生成一个订单编号
        order.setOrderSn(CodeGenerateUtils.generateOrderSn(searchMasterMsg.getUserId()));
        // 摘要
        order.setDigest("【"+searchMasterMsg.getTitle()+"】");
        // 最后确认时间 定时任务
        order.setLastcomfirmtime(new Date(System.currentTimeMillis()+2*24*60*60*1000));

        order.setState(0);
        order.setPetId(pet.getId());
        order.setPrice(searchMasterMsg.getPrice());
        order.setEmpId(dto.getHandler());
        order.setShopId(searchMasterMsg.getShopId());
        order.setUserId(searchMasterMsg.getUserId());
        order.setSearchMasterMsgId(dto.getMsgId());
        order.setAddress(searchMasterMsg.getAddress());

        return order;
    }

    private Pet SearchMasterMsg2Pet(SearchMasterMsg searchMasterMsg) {
        Pet pet = new Pet();
        pet.setName(searchMasterMsg.getName());
        pet.setCostprice(searchMasterMsg.getPrice());
        pet.setType_id(searchMasterMsg.getPetType());
        pet.setResources(searchMasterMsg.getResources());
        pet.setShop_id(searchMasterMsg.getShopId());
        pet.setSearch_master_msg_id(searchMasterMsg.getId());


        return pet;
    }
}
